home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / PostScriptFiles / StyleProcs.ps < prev    next >
Encoding:
Text File  |  2000-09-28  |  11.9 KB  |  619 lines  |  [TEXT/LTLK]

  1. %
  2. %    File:        StyleProcs.ps
  3. %
  4. %    This file contains the procedures used for Skia styles.
  5. %        This includes procedures and structures related to the augmented graphics state. 
  6. %
  7. %    Version:    Technology:    Quickdraw GX 1.1.x.
  8. %
  9. %    Copyright:    © 1991-7 by Apple Computer, Inc., all rights reserved.
  10. %
  11. %
  12. %        File contains the following procedures:
  13. %            SetPat
  14. %            CurrPat
  15. %            SetGridFit
  16. %            CurrGridFit
  17. %            SetBold
  18. %            CurrBold
  19. %            HasBold
  20. %            SetDash
  21. %            CurrDash
  22. %            SetRightIsOut
  23. %            CurrRightIsOut
  24. %            SetFrame
  25. %            CurrFrame
  26. %            SetStyle
  27. %            MakeStyleDict
  28. %            NewAugState
  29. %            AugGsave
  30. %            AugGrestore
  31. %            FullGsave
  32. %            FullGrestore
  33. %            CurrColorSet
  34. %            SetColorSet
  35. %            CurrOrMode
  36. %            SetOrMode
  37. %            SetBaseFont
  38. %            GetBaseFont
  39.  
  40.  
  41.  
  42. %<FF>
  43. %        The Augmented Graphics State dictionary, define the inital state:
  44. %            Has 2 entries in level-2, 4 entries in level-1
  45. %        Procedure NewAugGstate:  Creates a new augmented graphics state dictionary, containing
  46. %            Default values.  (no-bold, no pattern, no grid fitting)
  47. %
  48. %            - NewAugGstate dict
  49. %
  50. %                dict:            The new Augmented graphics state dictionary.
  51. %
  52. /NewAugGstate {
  53.  
  54.      11 dict dup begin
  55.  
  56.         /xBold 0 def
  57.         /yBold 0 def
  58.         /dash null def
  59.         /rightIsOut false def
  60.         /frameType 0 def
  61.         /FontMapping    [1 0 0 1 0 0] def            % The matrix that was applied to the current font.
  62.         /ColorSet null def                                    % Default color set space is null.
  63.         /orMode 0 def                                                % or-mode for 1 bit bitmaps?
  64.         /baseFont null def                                    % base font dictionary in augmented state.
  65.         /pattern null def                                        % pattern dictionary.
  66.         /gridFit false def
  67.  
  68.     end
  69.     
  70. } Bdef
  71.  
  72. /AugGstate NewAugGstate def            % Create an augmented graphics state in the main dictionary.
  73.  
  74. %
  75. %<FF>
  76. %
  77. %    Augmented graphics state save and restore operators and data structures:
  78. %
  79. /AugGsaveStack 31 array def                                %    31 entries, just like normal gsave.
  80.  
  81. % Make 31 dictionaries
  82. 0 1 30 {AugGsaveStack exch NewAugGstate put} for
  83.  
  84. /AugGsaveSP 0 def                                                    % Stack pointer for gsave stack.
  85.  
  86. %
  87. %        Procedure AugGsave:
  88. %        
  89. %        This procedure saves a copy of the current augmented graphics state on the stack.
  90. %
  91. /AugGsave {
  92.  
  93.     AugGsaveStack AugGsaveSP get                % Copy the current one into the top of the stack.
  94.     AugGstate
  95.     CopyDict
  96.     
  97.     /AugGsaveSP Inc                                            % Move the stack pointer
  98.  
  99. } Bdef
  100.  
  101. %
  102. %        Procedure AugGrestore:
  103. %        
  104. %        This procedure restores the augmented graphics state from the stack.
  105. %            If you call this with no AugGsaves on the stack, it screws up the Augmented Gstate dict.
  106. /AugGrestore {
  107.     
  108.     /AugGsaveSP Dec
  109.     AugGsaveSP 0 lt {/AugGsave 0 store} if
  110.     
  111.     AugGstate
  112.     AugGsaveStack AugGsaveSP get
  113.     CopyDict                                                    % Copy the one off the stack into current one.
  114.  
  115. } Bdef
  116.  
  117. %
  118. %    These two do a full gsave and grestore (Both mine and PostScript's)
  119. %        However, perserve the current font across grestores to minimize the
  120. %        need to do the oh, so expensive makefont/setfont operation.
  121. %
  122. /FullGsave {gsave AugGsave} Bdef
  123. /FullGrestore {AugGrestore currentfont grestore setfont} Bdef
  124.  
  125.  
  126. %
  127. %        QD2Grestore:  A grestore that maintains all of the style attributes that
  128. %            PostScript would otherwise restore, pretty much everything but color which
  129. %            The Imaging Engine maintains in a nested state.
  130. %
  131. /QD2Grestore {
  132.  
  133.     currentlinewidth
  134.     currentlinecap
  135.     currentlinejoin
  136.     currentmiterlimit
  137.     CurrGridFit
  138.     currentfont
  139.     currentdash
  140.     
  141.     grestore
  142.     
  143.     CurrPat null ne {
  144.         SynchPatMatrix
  145.     } if
  146.  
  147.     setdash    
  148.     setfont
  149.     SetGridFit
  150.     setmiterlimit
  151.     setlinejoin
  152.     setlinecap
  153.     setlinewidth
  154.  
  155. } Bdef
  156.  
  157.  
  158.  
  159. %<FF>
  160. %
  161. %    Augmented graphics state operators:
  162. %
  163.  
  164. %
  165. %        SetColorSet:  Sets the current color set.
  166. %            setDict CurrColorSet -
  167. %
  168. %                setDict:            An color set dictionary.
  169. %
  170. /SetColorSet {
  171.  
  172.     AugGstate /ColorSet 3 -1 roll put
  173.     
  174. } Bdef
  175.  
  176. %
  177. %
  178. %        CurrColorSet:  Get the current background color object
  179. %            CurrColorSet setDict
  180. %
  181. %                setDict:            An indexed color space dictionary.
  182. %
  183. /CurrColorSet {
  184.  
  185.     AugGstate /ColorSet get
  186.  
  187. } Bdef
  188.  
  189.  
  190. %
  191. %        SetBold:  Sets the bolding in the augmented graphics state.
  192. %            x y SetBold -
  193. %
  194. %                x,y:        The x and y boldness values.
  195. %
  196. /SetBold {
  197.  
  198.     AugGstate /yBold 3 -1 roll put
  199.     AugGstate /xBold 3 -1 roll put
  200.  
  201. } Bdef
  202.  
  203. %
  204. % CurrBold:    Returns the current boldness from the augmented graphics state.
  205. %
  206. %        - CurrBold xBold yBold
  207. %
  208. %                xBold:        x boldness value.
  209. %                yBold:        y boldness value.
  210.  
  211. /CurrBold {
  212.  
  213.     AugGstate /xBold get
  214.     AugGstate /yBold get
  215.     
  216. } Bdef
  217.  
  218. %
  219. %    HasBold:  A shortcut just to see if the current graphics state has boldness in it.
  220. %
  221. %        - HasBold bool
  222. %    
  223. %            bool:        true if the style contains non-zero boldness
  224. %
  225. /HasBold {
  226.  
  227.     CurrBold            % xBold yBold
  228.     0 ne                    % xBold bool
  229.     exch 0 ne            % bool bool
  230.     or                        % bool: if either xBold or yBold was non-zero.
  231.  
  232. } Bdef
  233.  
  234. %
  235. %        SetGridFit:  Sets the grid fitting in the graphics state.
  236. %            boolean SetGridFit -
  237. %
  238. %                boolean:        Grid fitting on or off.
  239. %
  240. languagelevel 2 lt {
  241.  
  242.     /SetGridFit {
  243.     
  244.         AugGstate /gridFit 3 -1 roll put
  245.  
  246.     } Bdef
  247.     
  248.     /CurrGridFit {
  249.     
  250.         AugGstate /gridFit get
  251.     
  252.     } Bdef
  253.  
  254. } {
  255.  
  256.     /SetGridFit /setstrokeadjust load def
  257.     /CurrGridFit /currentstrokeadjust load def
  258.     
  259. } ifelse
  260.  
  261.  
  262.  
  263. %
  264. %        SetPat:  Sets the pattern dictionary in the augmented graphics state.
  265. %            patternDict SetPat -
  266. %
  267. %                patternDict:        A valid pattern dictionary.
  268. %
  269.  
  270. /SetPat {
  271.     
  272.     AugGstate /pattern 3 -1 roll put
  273.  
  274. } Bdef
  275.  
  276. %
  277. %        CurrPat:  Sets the pattern dictionary in the augmented graphics state.
  278. %            CurrPat dict -
  279. %
  280. %                dict:        the current pattern dictionary.
  281. %
  282. /CurrPat {                    % Return the current pattern dictionary on the stack.
  283.  
  284.     AugGstate /pattern get
  285.  
  286. } Bdef
  287.  
  288.  
  289.  
  290. %<FF>
  291. %
  292. %        SetDash:        Sets the dashing in the augmented graphics state.
  293. %            dashDict SetDash -
  294. %    
  295. %            dashDict:        A valid dashing dictionary.
  296. %                or
  297. %            array phase SetDash -            (Just like setdash operator)  Data is from synonym
  298. %
  299. %  If the data is from a synonym, put a null in the
  300. %        Augmented graphics state for the dash dictionary
  301. %        because QD2Fill checks this to decide whether or not to
  302. %         do a DashStroke for Skia dashing or a normal stroke.
  303. %      If the dash is from a synonym, then a normal stroke does the correct thing.
  304. %
  305. /SetDash {
  306.  
  307.     dup null eq {                                    % If the dash is null, clear PostScript graphics state
  308.     
  309.         [] 0 setdash                                % Make sure there is no dash from a synonym
  310.         
  311.     } {                                                        % Else
  312.     
  313.         dup type /dicttype ne {                % If the first parameter wasn't a dictionary, data must be from synonym
  314.         
  315.             setdash                                                % Pass the synonym data to the setdash operator
  316.             null                                                    %    put a null on the stack to stick in Augmented graphics state
  317.             
  318.         } {                                                        % Else
  319.         
  320.             [] 0 setdash                                %  clear PostScript dash, information is in our dictionary.
  321.         
  322.         } ifelse
  323.         
  324.     } ifelse
  325.     
  326.     AugGstate /dash 3 -1 roll put        % Put dash dictionary into augmented graphics state.
  327.     
  328. } Bdef
  329.  
  330.  
  331. %
  332. %        CurrDash:        return the current dashing dictionary.
  333. %
  334. /CurrDash {
  335.  
  336.     AugGstate /dash get
  337.     
  338. } Bdef
  339.  
  340. %<FF>
  341. %
  342. %        SetFrame:        Sets the frame type in the augmented graphics state.
  343. %            frameType SetFrame -
  344. %    
  345. %            frameType:        0 for center, 1 for inside, -1 for outside
  346. %
  347. /SetFrame {
  348.  
  349.     AugGstate /frameType 3 -1 roll put
  350.     
  351. } Bdef
  352.  
  353.  
  354. %
  355. %        CurrFrame:        return the current dashing dictionary.
  356. %
  357. /CurrFrame {
  358.  
  359.     AugGstate /frameType get
  360.     
  361. } Bdef
  362.  
  363. %
  364. %        SetRightIsOut:        Sets the rightIsOut attribute in the augmented graphics state.
  365. %            bool SetRightIsOut -
  366. %    
  367. %            bool:        value of bit from Skia style.
  368. %
  369. /SetRightIsOut {
  370.  
  371.     AugGstate /rightIsOut 3 -1 roll put
  372.     
  373. } Bdef
  374.  
  375.  
  376. %
  377. %        CurrRightIsOut:        return the value of rightIsOut
  378. %
  379. /CurrRightIsOut {
  380.  
  381.     AugGstate /rightIsOut get
  382.     
  383. } Bdef
  384.  
  385.  
  386. %
  387. %        SetOrMode:        Sets the or mode attribute in the augmented graphics state.
  388. %            int SetOrMode -
  389. %    
  390. %            int:        0 means copy else 1 of the 4 ormodes. (0: copy 1: SrcOr, 2: SrcBic: 3: NotSrcOr, 4: NotSrcBic)
  391. %
  392. /SetOrMode {
  393.  
  394.     AugGstate /orMode 3 -1 roll put
  395.     
  396. } Bdef
  397.  
  398.  
  399. %
  400. %        CurrOrMode:        return the value of orMode
  401. %
  402. /CurrOrMode {
  403.  
  404.     AugGstate /orMode get
  405.     
  406. } Bdef
  407.  
  408.  
  409. %
  410. %
  411. %            Routines for getting imagemask colors and sense based on the or mode value.
  412. %
  413. %
  414. %            ImageMaskSense:
  415. %
  416. %            returns boolean to pass to imagemask (paint 1 or 0 bits ) based on ormode.
  417. %
  418. /ImageMaskSenseArray [true true true false false] def
  419. /ImageMaskSense {
  420.  
  421.     ImageMaskSenseArray CurrOrMode get
  422.  
  423. } Bdef
  424.  
  425. %
  426. %        ImageMaskColor:  Returns index of color in current color set to paint image mask with.
  427. %
  428. /ImageMaskColorArray [1 1 0 1 0] def
  429. /ImageMaskColor {
  430.  
  431.     ImageMaskColorArray CurrOrMode get
  432.  
  433. } Bdef
  434.  
  435.  
  436. %
  437. %        SetBaseFont: Sets the base font in the augmented graphics state.
  438. %            dict SetBaseFont -
  439. %
  440. %            dict:            a font dictionary.  Usually of 1pt font.
  441. %
  442. /SetBaseFont {
  443.  
  444.     AugGstate /baseFont 3 -1 roll put
  445.  
  446. } Bdef
  447.  
  448.  
  449. %
  450. %        CurrBaseFont:  Gets the current base font from the augmented graphics state.
  451. %            CurrBaseFont dict
  452. %
  453. %            dict:  The current base font dictionary.
  454. %
  455. /CurrBaseFont {
  456.  
  457.     AugGstate /baseFont get
  458.  
  459. } Bdef
  460.  
  461. %<FF>
  462. %
  463. %        The MakeStyleDict procedure:
  464. %            Procedure makes a dictionary representing the Skia style.
  465. %                Obviously, PostScriptable style attributes only, as described in ERS.
  466. %
  467. %            patternDict rightIsOut frameType dash pen cap join mitre gridFit textSize MakeStyleDict dict
  468. %
  469. %                patternDict:            null or a valid pattern dictionary.
  470. %                rightIsOut:                Boolean (rightIsOut bit from style attributes)
  471. %                frameType:                0 = center, 1 = inside, -1 = outside
  472. %                dash:                            null or a dashing array.
  473. %                pen:                            The pen thickness.
  474. %                cap:                            The line cap type.
  475. %                join:                            The line join type.
  476. %                mitre:                        The mitre limit.
  477. %                gridFit:                    (boolean) grid fitting or not.
  478. %                textSize:                    The text size from the style.
  479. %                dict:                            The dictionary created, left on the stack.
  480. %
  481. /MakeStyleDict {
  482.  
  483.     10 dict dup begin                            % Make the dictionary, leave copy on stack.
  484.         11 1 roll                                        % Put duplicate dictionary behind parameters.
  485.         
  486.         /textSize            Xdef
  487.         /gridFit            Xdef
  488.         /mitre                Xdef
  489.         /join                    Xdef
  490.         /cap                    Xdef
  491.         /pen                    Xdef
  492.         /dash                    Xdef
  493.         /frameType        Xdef
  494.         /rightIsOut        Xdef
  495.         /pattern             Xdef
  496.         
  497.     end
  498.  
  499. } Bdef
  500.  
  501. %
  502. %        The nilStyleDict is the style to use in a text face layer that had a nil outlineStyle field.
  503. %
  504. /nilStyleDict 10 dict dup
  505.     begin
  506.         /textSize 1 def
  507.         /gridFit false def
  508.         /mitre 1000 def
  509.         /join 0 def
  510.         /cap 0 def
  511.         /pen 0 def
  512.         /dash null def
  513.         /frameType 0 def
  514.         /rightIsOut F def
  515.         /pattern null def
  516.     
  517.     end
  518. def
  519.  
  520.  
  521. %<FF>
  522. %
  523. %        The SetStyle procedure:
  524. %            Procedure takes a Style Dictionary and applies its contents to  the current graphics state.
  525. %
  526. %        styleDict SetStyle -
  527. %
  528. %            styleDict:            A valid style dictionary.
  529. %
  530. /SetStyle {
  531.  
  532.     dup /dash get SetDash
  533.  
  534.     dup /pattern get SetPat
  535.     
  536.     dup /rightIsOut get SetRightIsOut
  537.  
  538.     dup /pen get setlinewidth
  539.  
  540.     dup /cap get setlinecap
  541.  
  542.     dup /join get setlinejoin
  543.  
  544.     dup /mitre get setmiterlimit
  545.     
  546.     dup /frameType get SetFrame
  547.  
  548.     /gridFit get SetGridFit
  549.     
  550.     
  551. } Bdef
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560. %<FF>
  561. %
  562. %                SynchPatMatrix:
  563. %
  564. %                In PostScript level-2, the pattern is bound to the matrix that was in the graphics
  565. %                state at the time the makepattern was executed.  In Skia, patterns map through whatever
  566. %                the shape's transform is (unless portAllignPattern is set in the styleAttributes)
  567. %
  568. %                In Level-1, we use this procedure to re-gridfit our vectors and mappings.
  569. %
  570. %                Execution of this procedure makes sure the pattern is synchronized to the CTM and should
  571. %                Be called whenever the CTM is modified. (MapCTM or any grestore type of operation)
  572. %
  573. languagelevel 1 gt {
  574.  
  575.     /SynchPatMatrix {
  576.     
  577.         CurrPat null ne {                            % If there is a pattern in augmented state.
  578.         
  579.             CurrPat dup 
  580.             /patTransform get makepattern            %     remake the pattern
  581.             SetPat                                                        %     put new one in augmented graphics state.
  582.                             
  583.         } if
  584.         
  585.     } Bdef
  586.  
  587.  
  588. } {                %% For level-1 just re-transform and re-grid the vectors.
  589.  
  590.     /SynchPatMatrix {
  591.     
  592.         CurrPat null ne {
  593.     
  594.             CurrPat begin
  595.             
  596.                 /patTransform [                                % Make the matrix by grid fitting parameters.
  597.                 
  598.                     ux uy Grid
  599.                     vx vy Grid
  600.                     px py Grid
  601.                 
  602.                 ] def
  603.                 
  604.                 /IpatTransform patTransform IpatTransform invertmatrix def
  605.                 
  606.                 % Reset the advance width vectors in the pattern font.
  607.                 
  608.                 patternFontDict /AdvanceVector get patTransform 0 get 0 exch put
  609.                 patternFontDict /AdvanceVector get patTransform 1 get 1 exch put
  610.                 
  611.         
  612.             end
  613.     
  614.         } if
  615.     
  616.     } def
  617.  
  618. } ifelse                % level-1 or level-2
  619.